@echo off
set keil_compiler_include_floder=%1
set full_name_path=%2
set linker_output_file_name=%3
::echo %keil_compiler_include_floder%
::echo %full_name_path%
::echo %linker_output_file_name%

%keil_compiler_include_floder%\..\bin\fromelf.exe --bin -o  "bin/" "%full_name_path%"
%keil_compiler_include_floder%\..\bin\fromelf.exe -acd --interleave=source -o "bin\%linker_output_file_name%.disasm" "%full_name_path%"
::..\..\..\tool\prepend_header\prepend_header.exe -r "..\..\..\tool\keys\rtk_rsa.pem" -t app_code -p "bin\%linker_output_file_name%.bin" -m 1 -c crc -a "..\..\..\tool\key.json"
..\..\..\tool\prepend_header\prepend_header.exe -t app_code -p "bin\%linker_output_file_name%.bin" -m 1 -c sha256 -a "..\..\..\tool\key.json"
..\..\..\tool\srec_cat "bin\%linker_output_file_name%.bin" -binary -offset 0x80E000 -o "bin\%linker_output_file_name%.hex" -intel
..\..\..\tool\MD5\md5.exe "bin\%linker_output_file_name%.bin"
..\..\..\tool\MD5\md5.exe "bin\%linker_output_file_name%_MP.bin"


::删除原来的固件
del "..\..\..\..\pack\%linker_output_file_name%_MP*.bin"
::获取固件大小
set /a app_bin_size_max=220*1024
set app_bin_path=./bin/app.bin
set app_bin_size=0
for %%i in (%app_bin_path%) do (
	set app_bin_size=%%~zi
)
::计算剩余大小
set /a remain=%app_bin_size_max%-%app_bin_size%
echo .\tool\after_build_common.bat : APP Image Size : remain=%remain% current=%app_bin_size% max=%app_bin_size_max%
if %remain% geq 0 (
	::复制新的固件
	copy  .\bin\%linker_output_file_name%_MP.bin ..\..\..\..\pack\%linker_output_file_name%_MP.bin
	..\..\..\tool\MD5\md5.exe "..\..\..\..\pack\%linker_output_file_name%_MP.bin"
	del "..\..\..\..\pack\%linker_output_file_name%_MP.bin"
	copy  .\bin\%linker_output_file_name%.trace ..\..\..\..\pack\%linker_output_file_name%.trace
)else (
	::固件大于flash分区最大值，报错，防止烧录后覆盖flash中其他分区
	echo .\tool\after_build_common.bat : error : APP Image size over size from flash_map.h!
)


del "bin\%linker_output_file_name%_MP.bin"